package com.chagee.channel.server.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.chagee.channel.server.event.service.LarkService;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author: chang.jiawei
 * @date: 2024/06/05
 */
@Component
@Slf4j
@Data
@RequiredArgsConstructor
public class ApolloRefreshConfig {

    private final RefreshScope refreshScope;
    private final LarkService larkService;

    @ApolloConfigChangeListener(value = {"ocr.yaml","lark.yaml"})
    public void onChange(ConfigChangeEvent changeEvent) {
        log.info("ConfigChangeEvent:{}--{}", changeEvent.getNamespace(), JSONUtil.toJsonStr(changeEvent.changedKeys()));
        refreshScope.refresh(OcrConfig.class);
        refreshScope.refresh(LarkConfig.class);

        refreshLarkApprovalSubscribe(changeEvent);
    }

    /**
     * 刷新飞书审批订阅
     * @param changeEvent
     */
    private void refreshLarkApprovalSubscribe(ConfigChangeEvent changeEvent){
        if(Objects.equals("lark.yaml", changeEvent.getNamespace())) {
            new Thread(()->{
                for (String changeKey : changeEvent.changedKeys()) {
                    if(changeKey.startsWith("lark.approvalCode.")){
                        ConfigChange configChange = changeEvent.getChange(changeKey);
                        log.info("ConfigChange: {}", configChange.toString());
                        try {
                            // 根据变更类型决定是订阅 or 取消订阅
                            switch (configChange.getChangeType()){
                                case ADDED:
                                    if(StrUtil.isNotEmpty(configChange.getNewValue())) {
                                        larkService.subscribeApproval(configChange.getNewValue());
                                    }
                                    break;
                                case DELETED:
                                    if(StrUtil.isNotEmpty(configChange.getOldValue())) {
                                        larkService.unsubscribeApproval(configChange.getOldValue());
                                    }
                                    break;
                                case MODIFIED:
                                    if(StrUtil.isNotEmpty(configChange.getOldValue())) {
                                        larkService.unsubscribeApproval(configChange.getOldValue());
                                    }
                                    if(StrUtil.isNotEmpty(configChange.getNewValue())) {
                                        larkService.subscribeApproval(configChange.getNewValue());
                                    }
                                    break;
                            }

                            Thread.sleep(1000);

                        } catch (Exception e) {
                            log.error("refreshLarkApprovalSubscribe fail", e);
                        }
                    }
                }
            }).start();
        }
    }

}
